#include<stdio.h>
#include<string.h>
#define N 50
char s[N];
 char* R0[]={"","I","II","III","IV","V","VI","VII","VIII","IX" }; 
 char* R1[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC" }; 
 char* R2[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM" }; 
 char* R3[]={"","M","MM","MMM"};
char* intToRoman(int num) {
	memset(s,0,N);
	strcat(s,R3[num/1000]);
	strcat(s,R2[num/100%10]); 
    strcat(s,R1[num/10%10]);
    strcat(s,R0[num%10]);
    return s; 
}
int romanToInt(char* s) {
	int j,i=0,n=strlen(s),x=0;
	 char* p;
	for (j=3;j>0;j--){
		p=R3[j];
		if (!memcmp(s+i,p,strlen(p))){	//s+i startsWith p
			x+=j*1000;
			i+=strlen(p);
			break;
		}	  
	}
	for (j=9;j>0;j--){
		p=R2[j];
		if (!memcmp(s+i,p,strlen(p))){
			x+=j*100;
			i+=strlen(p);
			break;
		}	  
	}
	for (j=9;j>0;j--){
		p=R1[j];
		if (!memcmp(s+i,p,strlen(p))){
			x+=j*10;
			i+=strlen(p);
			break;
		}	  
	}
	for (j=9;j>0;j--){
		p=R0[j];
		if (!memcmp(s+i,p,strlen(p))){
			x+=j;
			i+=strlen(p);
			break;
		}	  
	}
	
}
int test(int x){
	printf("%d=%s\n",x,intToRoman(x));
}
int main(){
	test(4);
	test(19);
	test(408);
	test(3999);
	int x;
	for (int i=4;i<43;i++){
		x=romanToInt(intToRoman(i));
		if (x!=i) printf("%s=%d,not %d error! \n",intToRoman(i),x,i);
	}
	
	return 0;
}